{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3285e18c-43b9-403a-b9b4-c1b9d364ebfe",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import GsSession, Environment\n",
    "from gs_quant.instrument import EqOption, OptionType, OptionStyle\n",
    "from gs_quant.backtests.strategy import Strategy\n",
    "from gs_quant.backtests.triggers import *\n",
    "from gs_quant.backtests.actions import *\n",
    "from gs_quant.backtests.equity_vol_engine import *\n",
    "from gs_quant.target.common import UnderlierType\n",
    "from datetime import date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b745f15f-2962-4930-b3f2-6c90aee2b79a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c167482-fd99-4a2b-8972-d235ca583c1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define backtest dates\n",
    "start_date = date(2019, 9, 4)\n",
    "end_date = date(2020, 9, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "997f3de3-1f8a-4f07-939c-f8ba74323351",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define option instruments for strategy\n",
    "instrument = EqOption('SX5E', underlierType=UnderlierType.BBID, expirationDate='3m', strikePrice='ATM', optionType=OptionType.Call, optionStyle=OptionStyle.European)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fc22284-6ef6-455d-bcfa-050583f5dd7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define a periodic trigger and action.  Trade and roll the option instrument every 1m\n",
    "trade_action = EnterPositionQuantityScaledAction(priceables=instrument, trade_duration='1m', trade_quantity=1000, trade_quantity_type=BacktestTradingQuantityType.quantity)\n",
    "trade_trigger = PeriodicTrigger(trigger_requirements=PeriodicTriggerRequirements(start_date=start_date, end_date=end_date, frequency='1m'), actions=trade_action)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee16b5f8-ada2-4ce1-bf71-508a2dcfe9ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create strategy\n",
    "strategy = Strategy(initial_portfolio=None, triggers=[trade_trigger])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b8a48e8-eb9b-49e2-a817-19acc28e20cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run backtest with SFK market model (the default). Other options are SD and SVR\n",
    "backtest = EquityVolEngine.run_backtest(strategy, start=start_date, end=end_date, market_model='SFK')\n",
    "pnl_sfk = backtest.get_measure_series(FlowVolBacktestMeasure.PNL)\n",
    "delta_sfk = backtest.get_measure_series(FlowVolBacktestMeasure.delta)\n",
    "gamma_sfk = backtest.get_measure_series(FlowVolBacktestMeasure.gamma)\n",
    "vega_sfk = backtest.get_measure_series(FlowVolBacktestMeasure.vega)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31536684-42dd-41d5-ad87-a287e484dfb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run backtest with SVR market model\n",
    "backtest = EquityVolEngine.run_backtest(strategy, start=start_date, end=end_date, market_model='SVR')\n",
    "pnl_svr = backtest.get_measure_series(FlowVolBacktestMeasure.PNL)\n",
    "delta_svr = backtest.get_measure_series(FlowVolBacktestMeasure.delta)\n",
    "gamma_svr = backtest.get_measure_series(FlowVolBacktestMeasure.gamma)\n",
    "vega_svr = backtest.get_measure_series(FlowVolBacktestMeasure.vega)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb3d4a7b-6a57-4519-bf9f-6495011464c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Market model does not affect performance calculation\n",
    "relative_pnl_diff = (pnl_svr - pnl_sfk)/pnl_sfk\n",
    "relative_pnl_diff.plot(legend=True, label='Relative PnL diff')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd264604-3072-4ade-881f-7b4ef1836525",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Market model affects delta and gamma calculations\n",
    "relative_delta_diff = (delta_svr - delta_sfk)/delta_sfk\n",
    "relative_delta_diff.plot(legend=True, label='Relative delta diff')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "585b3d09-4675-41ed-9032-39ccb76704a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "relative_gamma_diff = (gamma_svr - gamma_sfk)/gamma_sfk\n",
    "relative_gamma_diff.plot(legend=True, label='Relative gamma diff')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27a00835-d940-4960-9498-536298674603",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Market model does not affect the calculation of any other risk measures\n",
    "relative_vega_diff = (vega_svr - vega_sfk)/vega_sfk\n",
    "relative_vega_diff.plot(legend=True, label='Relative vega diff')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
